package com.woniuxy.service.impl;

import com.woniuxy.dao.UserDao;
import com.woniuxy.dao.impl.UserDaoDbImpl;
import com.woniuxy.entity.User;
import com.woniuxy.exception.UnameRegistedException;
import com.woniuxy.service.UserService;

import java.util.UUID;

public class UserServiceImpl implements UserService {

    UserDao userDao = new UserDaoDbImpl();

    @Override
    public User login(String uname, String password) {
        User user = userDao.getUserByUname(uname);
        if(user == null){
            throw new RuntimeException("用户名不存在");
        }
        if(!user.getPassword().equals(password)){
            throw new RuntimeException("密码错误");
        }
        return user;
    }

    @Override
    public void regist(User user) {
        //判断用户名是否被注册过
        if(userDao.getUserByUname(user.getUname())!=null){
            throw new UnameRegistedException("用户名被注册");
        }
        user.setUid(UUID.randomUUID().toString().replace("-", ""));
        userDao.addUser(user);
    }
}
